package com.googlecode.perfectable.webchat;

import static com.google.common.base.Preconditions.checkState;

import java.io.Serializable;

import javax.annotation.Nullable;

public class StandardMessage implements Message, Serializable {
	private static final class CopyWriter implements Message.Writer {
		@Nullable
		private String writtenBody;

		@Override
		public void writeBody(String body) {
			this.writtenBody = body;
		}

		public StandardMessage createCopy() {
			checkState(this.writtenBody != null);
			return new StandardMessage(this.writtenBody);
		}
	}

	private static final long serialVersionUID = 8553796474063352136L;

	private final String body;
	
	public static StandardMessage of(String body) {
		return new StandardMessage(body);
	}

	private StandardMessage(String body) {
		this.body = body;
	}

	@Override
	public void write(Message.Writer writer) {
		writer.writeBody(this.body);
	}

	public static StandardMessage copy(Message event) {
		@SuppressWarnings("synthetic-access")
		CopyWriter copyWriter = new CopyWriter();
		event.write(copyWriter);
		return copyWriter.createCopy();
	}

}
